"""
@File: error_code.py
@Project: 约个球
@Author: zhangjian
@Time: 2022.8.27 16:33
@Description: None
"""
from werkzeug.exceptions import HTTPException


class APIException(HTTPException):
    code = 500  # http状态码
    message = "错误"  # 状态描述信息
    status_code = 10001  # 程序状态

    def __init__(self, message=None, code=None, status_code=None):
        if message:
            self.message = message
        if code:
            self.code = code
        if status_code:
            self.status_code = status_code
        super(APIException, self).__init__()

    def get_body(
            self,
            environ=None,
            scope=None,
    ) -> str:
        body = dict(
            message=self.message,
            status_code=self.status_code
        )
        import json
        content = json.dumps(body)
        return content

    def get_headers(
            self,
            environ=None,
            scope=None,
    ):
        return [('content-Type', 'application/json')]


class APIAuthorizedException(APIException):
    message = "API授权认证失败"
    status_code = 10002
    code = 401


class FormValidateException(APIException):
    message = "表单验证失败"
    status_code = 10003
    code = 401


class TokenFailException(APIException):
    message = "token不合法，验证失败"
    status_code = 10004
    code = 401

class PasswordValidateException(APIException):
    message = "密码错误"
    status_code = 10005
    code = 401

class EmailValidateException(APIException):
    message = "此邮箱还未注册"
    status_code = 10005
    code = 401

class EmailAlreadyExistException(APIException):
    message = "邮箱已存在"
    status_code = 10006
    code = 401